package cn.zyzpp.controller.job;

import cn.zyzpp.entity.job.JobClass;
import cn.zyzpp.entity.job.NewJob;
import cn.zyzpp.entity.job.UserJob;
import cn.zyzpp.service.job.JobService;
import cn.zyzpp.service.job.UserJobService;
import cn.zyzpp.service.user.UserService;
import cn.zyzpp.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Create by yster@foxmail.com 2018/5/22/022 16:50
 */
@Controller
@RequestMapping("/job")
public class JobController {
    @Autowired
    private JobService jobService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserJobService userJobService;
    @Autowired
    private HttpSession session;


    @RequiresAuthentication
    @RequestMapping(value = "/save")
    public String save(NewJob job, ModelMap modelMap){
        try {
            job.setJobDate(new Date());
            job.setJobGood(job.getJobGood() != null ? StringUtil.symline(job.getJobGood()) : "");
            job.setJobDetail(job.getJobDetail() != null ? StringUtil.symline(job.getJobDetail()) : "");
            job.setJobComDetail(job.getJobComDetail() != null ? StringUtil.symline(job.getJobComDetail()) : "");
            jobService.save(job, StringUtil.getIntByString(session.getAttribute("uid")));
            modelMap.addAttribute("or", 1);
            modelMap.addAttribute("message", "发布成功！");
        }catch (Exception e) {
            modelMap.addAttribute("or", 0);
            modelMap.addAttribute("message", "发布失败！");
        }
        return "success";
    }

    /**
     * 更新招聘
     * @param job
     * @return
     */
    @ResponseBody
    @RequiresAuthentication
    @RequestMapping(value = "/updata")
    public String updata(NewJob job){
        jobService.updata(job);
        return "更新成功";
    }

    /**
     * 删除发布的招聘
     * @param id
     * @return
     */
    @ResponseBody
    @RequiresAuthentication
    @RequestMapping(value = "/delect/{id}")
    public String delectJob(@PathVariable Integer id){
        if(userJobService.queryByJid(id).size()>0){
            return "已有人应聘不可删除";
        }
        jobService.delect(id);
        return "删除成功";
    }

    /**
     * 关闭招聘
     * @param id
     * @param or
     * @return
     */
    @ResponseBody
    @RequiresAuthentication
    @RequestMapping(value = "/close/{id}/{or}")
    public String closeJob(@PathVariable Integer id,@PathVariable Integer or){
        // or：1公开 0关闭 需要权限验证
        if (or == 1) {
            jobService.updataOrUse(id, 0);
            return "关闭成功";
        }
        if(or == 0) {
            jobService.updataOrUse(id, 1);
            return "重新发布成功";
        }
        return "不知道";
    }

    /**
     * 工作详情页
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/job")
    public String getJob(int id, ModelMap modelMap){
        if (jobService.query(id) == null){
            modelMap.addAttribute("job",null);
            return "job";
        }
        NewJob job = jobService.query(id);
        job.setImgUrl(userService.queryUserImg(job.getUid()).getImgUrl());
        modelMap.addAttribute("job",job);
        return "job";
    }

    /**
     * 投简历
     * @param id
     * @return
     */
    @ResponseBody
    @RequiresAuthentication
    @RequestMapping(value = "/result",method = RequestMethod.POST)
    public String touJob(int id){
        NewJob job = jobService.query(id);
        if (job == null){
            return "职位不存在";
        }
        int uid = StringUtil.getIntBySessionUid(session);
        if (job.getOrUse()!=1){
            return "该招聘已关闭";
        }
        if (uid == job.getUid()){
            return "自己不能投递自己发布的工作";
        }
        if (userJobService.queryByUidAndPid(uid,id) != null ){
            return "请勿重复投递";
        }
        UserJob userJob = new UserJob(uid,id,"投递成功",new Date(),job.getJobClass(),job.getJobComName()
        ,job.getJobWeekDay(),job.getJobMoney(),job.getJobPosition());
        userJobService.save(userJob);
        jobService.updataCount(id);//更新热度
        return "投递成功";
    }

    /**
     * 发布招聘职位
     * @return
     */
    @RequestMapping("/issue")
    @RequiresAuthentication
    public String issuePosiTion(ModelMap model){
        model.addAttribute("joblist",JobClass.getJobList());
        return "position";
    }

    /**
     * 撤销投递
     * @param jid
     * @return
     */
    @ResponseBody
    @RequestMapping("delectUJ/{jid}")
    @RequiresAuthentication
    public String userJob(@PathVariable int jid){
        int uid = StringUtil.getIntBySessionUid(session);
        try {
            userJobService.delectByUidAndJid(uid, jid);
            return "删除成功";
        }catch (Exception e){
            e.printStackTrace();
            return "删除失败";
        }
    }
    /**
     * 更新求职状态
     */
    @ResponseBody
    @RequiresAuthentication
    @RequestMapping("/updataUJ/{uid}/{jid}")
    public String updataUserJob(String state,@PathVariable int uid,@PathVariable int jid){
        try {
            userJobService.updataUJSatate(uid, jid, state);
            return "更新成功";
        }catch (Exception e){
            return "更新失败";
        }
    }

}
